package com.mag.servlet;

import com.mag.domain.Suppliers;
import com.mag.service.SuppliersService;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static java.lang.Double.parseDouble;
import static java.lang.Integer.parseInt;

public class Suppliersu extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

        response.setContentType("text/html");
        SuppliersService Ss = new SuppliersService();
        String type = request.getParameter("type");

        if ("goUpdate".equals(type)) {
            String supplierid = request.getParameter("supplierid");
            Suppliers supplier = Ss.GetSupplierBySupplierID(supplierid);
            request.getSession().setAttribute("updateSupplier", supplier);
            response.sendRedirect("/manager/jsp/update.jsp");
        } else if ("Update".equals(type)) {
            Suppliers suppliers = new Suppliers();

            suppliers.setSupplierID(request.getParameter("supplierid"));
            suppliers.setSupplierName(request.getParameter("SupplierName"));
            suppliers.setSupplierAddress(request.getParameter("SupplierAddress"));
            suppliers.setWebSite(request.getParameter("WebSite"));
            suppliers.setSupplierPhone(request.getParameter("SupplierPhone"));
            suppliers.setFaxSupplier(request.getParameter("FaxSupplier"));
            suppliers.setOfficialEmail(request.getParameter("OfficialEmail"));
            suppliers.setPersonContact(request.getParameter("PersonContact"));
            suppliers.setPhoneContact(request.getParameter("PhoneContact"));
            suppliers.setDateBulid(request.getParameter("DateBulid"));
            suppliers.setGoodsSupply(request.getParameter("GoodsSupply"));
            suppliers.setDateFirstSupply(request.getParameter("DateFirstSupply"));
            suppliers.setAccumulativeTime(parseInt(request.getParameter("AccumulativeTime")));
            suppliers.setAccumulativeLevel(parseInt(request.getParameter("AccumulativeLevel")));
            suppliers.setNumFault(parseInt(request.getParameter("NumFault")));
            suppliers.setNumBacklogging(parseInt(request.getParameter("NumBacklogging")));
            suppliers.setTotalMoney(parseDouble(request.getParameter("TotalMoney")));
            suppliers.setNoFaultRate(parseDouble(request.getParameter("NoFaultRate")));
            suppliers.setDeliveryOnTime(parseDouble(request.getParameter("DeliveryOnTime")));
            if (Ss.UpdateSupplier(suppliers)) {
                response.sendRedirect("/manager/Suppliersu?type=goUpdate&supplierid=" + suppliers.getSupplierID());
            }
        } else if ("delete".equals(type)) {
            String supplierid = request.getParameter("supplierid");
            if (Ss.DelSupplier(supplierid)) {
                response.sendRedirect("/manager/SearchSupplier?type=allsupplier");
            }
        } else if ("goDetail".equals(type)) {
            String supplierid = request.getParameter("supplierid");
            Suppliers supplier = Ss.GetSupplierBySupplierID(supplierid);
            request.getSession().setAttribute("detailSupplier", supplier);
            response.sendRedirect("/manager/jsp/details.jsp");
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {

        this.doGet(request, response);
    }

}